/**
 * 申请单、报销单、补助单 tab列表封装
 * @author hongwei 2018-09-28
 */
(function(window){
    'use strict';

    var order = function (params) {
        $.extend(this.params, params);
        this._init();
    };

    order.prototype = {

        //参数
        params : {
            orderName               : ''     //xx单名称
            ,mineListInterface      : ''     //我的xx单接口
            ,otherListInterface     : ''     //待我审批的xx单接口
            ,cancelInterface        : ''     //取消接口
            ,invalidInterface       : ''     //作废接口
            ,winName                : ''     //打开xx单新窗口的名称
            ,storageId              : ''     //存储xx单的storageId的名称
        }

        //页面初始化
        , _init : function () {
            var that = this;

            that.travelApplyInfo(); //step1:点击进入详情
            that.initLoadData();    //step2:初始加载的数据
            that.tabClick();        //step3:tab标签点击
            that.cancel();          //step4:申请单取消
            that.invalid();         //step5:申请单作废
        }

        //step1:点击进入详情
        ,travelApplyInfo : function () {
            var that = this;

            $('body').on('click', '.travel_info', function () {
                var id = $(this).data('id');
                $api.setStorage(that.params.storageId, id);
                api.openWin({
                    name: that.params.winName,
                    url: that.params.winName+'.html',
                    slidBackEnabled: false
                });
            });
        }

        //step2:初始加载的数据
        ,initLoadData : function () {
            var that = this;
            that.loadData(that.params.mineListInterface);
        }

        //step3:tab标签点击
        ,tabClick : function () {
            var that = this;
            //初始化url接口参数
            var loadUrl;
            $(".aui-tab .aui-tab-item").click(function(){
                $(".aui-tab .aui-tab-item").removeClass('aui-active');
                $(this).addClass('aui-active');
                var index = $(".aui-tab .aui-tab-item").index($(this));
                if(index === 0){
                    loadUrl = that.params.mineListInterface;    //我的xx单接口
                }else{
                    loadUrl = that.params.otherListInterface;   //待我审批的xx单接口
                }
                that.loadData(loadUrl);
            });
        }

        //step4:xx单取消
        ,cancel : function () {
            var that = this;
            $('body').on('click', '.cancel', function(){
                api.confirm({
                    title: '确定取消'+that.params.orderName+'订单吗',
                    buttons: ['确定', '取消']
                }, function(ret, err) {
                    var index = ret.buttonIndex;
                    if (index === 1) {
                        var id = $(this).data('id');
                        var param = {};
                        param.url = that.params.cancelInterface;
                        param.data = {
                            id: id
                        };
                        ajax(param, function(ret) {
                            if (ret.code === 1) {
                                $alert('取消成功',function(){
                                    location.reload();
                                });
                            } else {
                                $toast('取消失败');
                            }
                        })
                    }
                });
            });
        }

        //step5:申请单作废
        ,invalid : function () {
            var that = this;

            $('body').on('click', '.invalid', function () {
                api.confirm({
                    title: '确定取消'+that.params.orderName+'吗',
                    buttons: ['确定', '取消']
                }, function(ret, err) {
                    var index = ret.buttonIndex;
                    if (index === 1) {
                        var id = $(this).data('id');
                        var param = {};
                        param.url = that.params.invalidInterface;
                        param.data = {
                            id: id
                        };
                        ajax(param, function(ret) {
                            if (ret.code === 1) {
                                $alert('作废成功',function(){
                                    location.reload();
                                });
                            } else {
                                $toast('作废失败');
                            }
                        })
                    }
                });
            });
        }

        //step11:加载数据
        ,loadData : function (loadUrl) {

            //step1:使用变量
            var page = 0;   //当前页数
            var max_page;   //最大页数

            //step2:重置
            $("#tab").empty();

            //step3:使用插件加载
            $('#tab').dropload({
                scrollArea: window,
                domDown: {
                    domNoData: '<div class="dropload-noData">已经到底了</div>'
                },
                loadDownFn: function(me) {
                    var page_num = (page == undefined) ? 1 : parseInt(++page);
                    var param = {};

                    if (page_num > max_page && page_num != null && page_num != undefined) {
                        me.lock();
                        me.noData();
                        me.resetload();
                        return false;
                    }
                    param.url = loadUrl;
                    param.data = {
                        page: page
                    };
                    /*请求申请单列表接口*/
                    ajax(param, function(ret) {
                        if (ret.code === 1) {
                            if (ret.data.data.length === 0) {
                                $toast('暂无内容');
                                me.lock();
                                me.noData();
                                me.resetload();
                                return false;
                            } else {
                                var interText = doT.template($("#list_tpl").text());
                                $("#tab").append(interText(ret.data.data));
                                page = ret.data.current_page;
                                max_page = parseInt(ret.data.last_page);
                                api.parseTapmode();
                                me.resetload();
                            }
                        } else {
                            $("#tab").text('');
                            $toast(ret.msg);
                            me.lock();
                            me.noData();
                            me.resetload();
                        }
                    })
                }
            });
        }
    };

    window.order = order;
})(window);
